package com.pet.system.service.impl;

import com.pet.common.utils.DateUtils;
import com.pet.system.domain.PetCart;
import com.pet.system.mapper.PetCartMapper;
import com.pet.system.service.IPetCartService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 购物车Service业务层处理
 *
 * @author pet
 * @date 2022-10-21
 */
@Service
public class PetCartServiceImpl implements IPetCartService {
    @Autowired
    private PetCartMapper petCartMapper;

    /**
     * 查询购物车
     *
     * @param id 购物车主键
     * @return 购物车
     */
    @Override
    public PetCart selectPetCartById(Long id) {
        return petCartMapper.selectPetCartById(id);
    }

    /**
     * 查询购物车列表
     *
     * @param petCart 购物车
     * @return 购物车
     */
    @Override
    public List<PetCart> selectPetCartList(PetCart petCart) {
        return petCartMapper.selectPetCartList(petCart);
    }

    /**
     * 新增购物车
     *
     * @param petCart 购物车
     * @return 结果
     */
    @Override
    public int insertPetCart(PetCart petCart) {
        PetCart petCartSelect = petCartMapper.selectPetCartWx(petCart);
        if (ObjectUtils.isEmpty(petCartSelect)) {
            petCart.setCreateTime(DateUtils.getNowDate());
            return petCartMapper.insertPetCart(petCart);
        } else {
            petCart.setId(petCartSelect.getId());
            petCart.setQuantity(petCartSelect.getQuantity() + 1);
            return petCartMapper.updatePetCart(petCart);
        }
    }

    /**
     * 修改购物车
     *
     * @param petCart 购物车
     * @return 结果
     */
    @Override
    public int updatePetCart(PetCart petCart) {
        petCart.setUpdateTime(DateUtils.getNowDate());
        return petCartMapper.updatePetCart(petCart);
    }

    /**
     * 批量删除购物车
     *
     * @param ids 需要删除的购物车主键
     * @return 结果
     */
    @Override
    public int deletePetCartByIds(Long[] ids) {
        return petCartMapper.deletePetCartByIds(ids);
    }

    /**
     * 删除购物车信息
     *
     * @param id 购物车主键
     * @return 结果
     */
    @Override
    public int deletePetCartById(Long id) {
        return petCartMapper.deletePetCartById(id);
    }
}
